﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport.Controls;
using System.Windows.Data;
using ReflectionStudio.Controls;
namespace WPFExcelReport
{

    public partial class TQPMERKM : ViewBase
    {
        #region ==MERKM===========
        public static readonly DependencyProperty MERKMProperty = DependencyProperty.Register("MERKM",
            typeof(string), typeof(TQPMERKM),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMERKMChanged));
        public string MERKM
        {
            get
            {
                return (string)GetValue(MERKMProperty);
            }
            set
            {
                SetValue(MERKMProperty, value);
                ((VQPMERKM)MV).MERKM = value;
                LOADQT();
            }
        }
        private static void OnMERKMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPMERKM;
            if (src != null)
            {
                src.MERKM = (string)e.NewValue;
            }
        }
        #endregion

        #region ==DENUM===========
        public static readonly DependencyProperty DENUMProperty = DependencyProperty.Register("DENUM",
            typeof(string), typeof(TQPMERKM),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDENUMChanged));
        public string DENUM
        {
            get
            {
                return (string)GetValue(DENUMProperty);
            }
            set
            {
                SetValue(DENUMProperty, value);
                if (IsEdit && dwQT != null)
                {
                    dwQT["QPMMEEH"] = value;
                }
            }
        }
        private static void OnDENUMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPMERKM;
            if (src != null)
            {
                src.DENUM = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QTM===========
        public static readonly DependencyProperty QTMProperty = DependencyProperty.Register("QTM",
            typeof(string), typeof(TQPMERKM),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQTMChanged));
        public string QTM
        {
            get
            {
                return (string)GetValue(QTMProperty);
            }
            set
            {
                SetValue(QTMProperty, value);
                if (IsEdit && dwQT != null)
                {
                    string bez = dwQT["QPMBEZ"].ToString().Trim();
                    dwQT["QPMBEZ"] =Helpers.Helper.Left(bez+"+"+ value.PadRight(50),50);
                }
            }
        }
        private static void OnQTMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPMERKM;
            if (src != null)
            {
                src.QTM = (string)e.NewValue;
            }
        }
        #endregion


        #region ==NORM===========
        public static readonly DependencyProperty NORMProperty = DependencyProperty.Register("NORM",
            typeof(string), typeof(TQPMERKM),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNORMChanged));
        public string NORM
        {
            get
            {
                return (string)GetValue(NORMProperty);
            }
            set
            {
                SetValue(NORMProperty, value);
                ((VQPMERKM)MV).NORM = value;
                if (IsEdit && dwQT != null)
                {
                    dwQT["QPMNORM"] = value;
                }
            }
        }
        private static void OnNORMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPMERKM;
            if (src != null)
            {
                src.NORM = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwQT===========
        public static readonly DependencyProperty dwQTProperty = DependencyProperty.Register("dwQT",
            typeof(DataRow), typeof(TQPMERKM),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQTChanged));
        public DataRow dwQT
        {
            get
            {
                return (DataRow)GetValue(dwQTProperty);
            }
            set
            {
                SetValue(dwQTProperty, value);
            }
        }
        private static void OdwQTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPMERKM;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQT = row;
            }
        }
        #endregion

        public TQPMERKM()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VQPMERKM();
            MESSAGES += ";";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQT = dwMaster;
            if (dwQT == null)
            {
                DENUM = "";
            }
            else
            {
                DENUM = dwQT["QPMMEEH"].ToString();
            }
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;

        }

        public void LOADQT()
        {
            if (MERKM.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            DataRow dwData = TBMain.NewRow();
            dwData["QPMMANDANT"] = app.Mandant;
            dwData["QPMMERKM"] = MERKM;
            DataSet SResult = ((VQPMERKM)MV).QueryQPMERKM(dwData, 3);
            MergeData(SResult, true);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwQT == null)
                return;
            dwQT["QPMAENB"] = app.User;
            dwQT["QPMAEND"] = vm.date;
            ((VQPMERKM)MV).UpdateQPMERKM(dwQT, app.company, 0);
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwQT == null)
                return;
            ((VQPMERKM)MV).DeleteQPMERKM(dwQT, 1);
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwQT == null)
                return;
            ((VQPMERKM)MV).COPYQT(dwQT);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (ART.Trim().Length == 0)
            {
                MessageBox.Show("请输入项目类型");
                return;
            }
            ((VQPMERKM)MV).NEWQT();
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }

        public List<object> QTINPUT()
        {
            NOTConverter not = new NOTConverter();
            Divideby1000 d1000 = new Divideby1000();

            List<object> lc = new List<object>();
            dwData = MV.SetProd.Tables["QPMERKM"].NewRow();
            dwData["QPMMANDANT"] = app.Mandant;
            SResult = ((VQPMERKM)MV).QueryQPMERKM(dwData, -2);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string QT = row["QPMMERKM"].ToString();
                Grid grid = new Grid();
                ColumnDefinition cd;
                cd = new ColumnDefinition();
                cd.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(cd);
                cd = new ColumnDefinition();
                cd.Width =new GridLength(100);
                grid.ColumnDefinitions.Add(cd);
                TextBlock txt = new TextBlock();
                txt.Text = QT + ":";
                txt.SetValue(Grid.ColumnProperty, 0);
                grid.Children.Add(txt);
                NumericBox nb = new NumericBox();
                nb.SetValue(Grid.ColumnProperty, 1);
                nb.DecimalPlaces = 2; 

                Binding b = new Binding();
                b.Path = new PropertyPath("IsEdit");
                b.ElementName = "TV";
                b.Converter = not;
                nb.SetBinding(NumericBox.IsReadOnlyProperty, b);  

                b = new Binding(); 
                b.Path = new PropertyPath(QT.Trim()); 
                b.Converter = d1000;
                nb.SetBinding(NumericBox.TextProperty, b);
                lc.Add(grid);
            }
            return lc;

            /*
               <Grid Margin="10,0,0,0">
                   
                     <TextBlock  Grid.Column="0" Text="AB2:"></TextBlock>
                     <ex:NumericBox  Grid.Column="1"   DecimalPlaces="2"  
                                     IsReadOnly="{Binding IsEdit,ElementName=TV,Converter={StaticResource not}}"
                                     Text="{Binding AB2, Source={StaticResource vwMaster},Converter={StaticResource d1000}}" Width="100" />
                 </Grid>*/
        }
    }
}
